package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.mapper.CulturePartDiscussionMapper;
import com.graduation.springbootinit.model.entity.CulturePart.CulturePartDiscussion;
import com.graduation.springbootinit.service.CulturePartDiscussionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 文化讨论区服务实现类
 */
@Service
public class CulturePartDiscussionServiceImpl extends ServiceImpl<CulturePartDiscussionMapper, CulturePartDiscussion>
        implements CulturePartDiscussionService {

    @Override
    public long createDiscussion(CulturePartDiscussion discussion, long userId) {
        if (discussion == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isBlank(discussion.getTitle())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题不能为空");
        }
        if (StringUtils.isBlank(discussion.getContent())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容不能为空");
        }
        discussion.setUserId(userId);
        discussion.setViewCount(0);
        discussion.setLikeCount(0);
        discussion.setCommentCount(0);
        discussion.setStatus(0);
        boolean result = this.save(discussion);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return discussion.getId();
    }

    @Override
    public boolean deleteDiscussion(long id, long userId) {
        CulturePartDiscussion discussion = this.getById(id);
        if (discussion == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        if (discussion.getUserId() != userId) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        return this.removeById(id);
    }

    @Override
    public boolean updateDiscussion(CulturePartDiscussion discussion, long userId) {
        if (discussion == null || discussion.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CulturePartDiscussion oldDiscussion = this.getById(discussion.getId());
        if (oldDiscussion == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        if (oldDiscussion.getUserId() != userId) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        if (StringUtils.isNotBlank(discussion.getTitle())) {
            oldDiscussion.setTitle(discussion.getTitle());
        }
        if (StringUtils.isNotBlank(discussion.getContent())) {
            oldDiscussion.setContent(discussion.getContent());
        }
        return this.updateById(oldDiscussion);
    }

    @Override
    public CulturePartDiscussion getDiscussionById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return this.getById(id);
    }

    @Override
    public void incrementViewCount(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        baseMapper.incrementViewCount(id);
    }

    @Override
    public void incrementCommentCount(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        baseMapper.incrementCommentCount(id);
    }

    @Override
    public Page<CulturePartDiscussion> page(Page<CulturePartDiscussion> page, QueryWrapper<CulturePartDiscussion> queryWrapper) {
        if (page == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return baseMapper.selectPage(page, queryWrapper);
    }
}